<template>
  <el-container>
    <el-header id="header" class="navbar navbar-inverse navbar-fixed-top">
      <nav class="container">
        <div class="navbar-collapse">
          <a href="/">
            <div class="navbar-brand">Submit</div>
          </a>
          <el-menu
            :default-active="activeIndex2"
            class="el-menu-demo"
            mode="horizontal"
            @select="handleSelect"
            text-color="#fff"
            active-text-color="#e74c3c"
            background-color="#3d4450"
          >
            <el-submenu index="1">
              <template slot="title">任务管理</template>
              <el-menu-item index="1-1">任务列表</el-menu-item>
              <el-menu-item index="1-2">新建任务</el-menu-item>
              <el-menu-item index="1-3">任务状态</el-menu-item>
            </el-submenu>
            <el-submenu index="2">
              <template slot="title">提交人管理</template>
              <el-menu-item index="2-1">提交人名单</el-menu-item>
              <el-menu-item index="2-2">新建提交人</el-menu-item>
            </el-submenu>
            <el-submenu index="3">
              <template slot="title">标签管理</template>
              <el-menu-item index="3-1">新建标签</el-menu-item>
            </el-submenu>
            <el-submenu index="4">
              <template slot="title">关于submit</template>
              <el-menu-item index="4-1">组员简介</el-menu-item>
            </el-submenu>
          </el-menu>
        </div>
      </nav>
    </el-header>
    <el-main id="boder">
      <div class="container">
        <div class="row">
          <el-col id="mainstay" :span="16">
            <div class="ct">
              <div class="taglist">
                <el-col
                  v-for="(item, index) in tags"
                  :key="item"
                  :offset="index / index"
                  :span="4"
                >
                  <span
                    class="tag sub_tag"
                    :class="[
                      curTag === item.value ? 'tag_active' : 'tag_unactive',
                    ]"
                  >
                    <i class="el-icon-collection-tag"></i>
                    <a
                      href="#"
                      @click.prevent="selectTag(item.id, item.value)"
                      >{{ item.value }}</a
                    >
                  </span>
                </el-col>
              </div>
              <div class="mn" style="margin-top: 50px; background: #fff">
                <el-row
                  v-for="(item, index) in tasks"
                  :key="item"
                  :offset="index"
                  class="task-item"
                >
                  <div class="tc">
                    <div class="th">
                      <h3>{{ item.title }}</h3>
                    </div>
                    <div class="tbtn">
                      <a
                        href="#"
                        @click.prevent="
                          optTask(item.id, item.status, item.stime, item.etime)
                        "
                        class="btn"
                        ><i
                          :class="[
                            item.status === '进行中'
                              ? 'el-icon-video-pause'
                              : 'el-icon-video-play',
                          ]"
                        ></i>
                        <span>{{
                          item.status === "进行中" ? "暂停" : "运行"
                        }}</span></a
                      >
                      <a href="#" class="btn" @click="editName(item.id)"
                        ><i class="el-icon-edit"></i> <span>编辑</span></a
                      >
                      <a href="#" @click.prevent="settask(item.id)" class="btn"
                        ><i class="el-icon-reading"></i> <span>详情</span></a
                      >
                    </div>
                  </div>
                </el-row>
              </div>
            </div>
          </el-col>

          <el-col id="sidebar" :span="4">
            <aside id="task-login">
              <div class="panel task-panel">
                <div class="panel-heading">
                  <i class="el-icon-sort-up"></i>
                  欢迎!
                  <i class="el-icon-sort-down"></i>
                </div>
                <div class="login-panel text-center">
                  <img
                    src="https://cache.cswsadlab.com/wp-content/uploads/2015/03/笑颜1-150x150.jpg"
                    alt="头像"
                    class="avatar avatar-60 wp-user-avatar wp-user-avatar-60 photo avatar-default"
                    width="60"
                    height="60"
                  />
                  <a class="user-name" href="https://vcb-s.com/wp-admin/"
                    >kamike</a
                  >
                  <a
                    class="btn btn-inverse-primary"
                    href="#"
                    @click.prevent="quit()"
                    title="退出登录"
                    >退出登录</a
                  >
                </div>
              </div>
            </aside>
          </el-col>
        </div>
      </div>
    </el-main>
    <el-footer id="task-footer">
      <div class="container">
        我们的目标是征服星河大海 Powered By
        <a href="/test" target="_blank">Submit</a>
      </div>
    </el-footer>
  </el-container>
</template>

<script>
import { colTaskList } from "@/api/brand.js";
import { formatDate } from "@/utils/dateformat.js";
import { countByStatus } from "@/api/brand.js";
import { fetchTaskWithStatus } from "@/api/brand.js";
export default {
  data() {
    return {
      activeIndex: "1",
      activeIndex2: "1",
      cid: 1,
      tasks: [],
      list: [],
      tags: [],
      started: null,
      ended: null,
      paused: null,
      curTag: "",
    };
  },
  created() {
    //this.cid = this.$route.query.cid;
    this.getTasks();
    this.getTags();
  },
  methods: {
    opttask(tid, status, stime, etime) {
      var curTime = new Date();
      var endTime = new Date(Date.parse(etime));
      if (status === "未开始") {
      } else if (status === "进行中") {
        if (endTime > curTime) {
          this.$confirm("该任务还未到截至时间，是否暂停", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {})
            .catch(() => {});
        }
      } else {
        this.$confirm("该任务已经截至，是否重新开启任务", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {

            })
            .catch(() => {

            });
      }
    },
    settask(tid) {
      this.$router.push({
        path: "/settask",
        query: {
          cid: this.cid,
          tid: tid,
        },
      });
    },
    getTags() {
      let params1 = new URLSearchParams();
      params1.append("cid", this.cid);
      params1.append("status", 1);
      countByStatus(params1).then((response) => {
        this.started = response.data;
        let item = {};
        item.id = 1;
        item.value = "进行中 " + this.started;
        this.tags.push(item);
      });
      let params2 = new URLSearchParams();
      params2.append("cid", this.cid);
      params2.append("status", 2);
      countByStatus(params2).then((response) => {
        this.ended = response.data;
        let item = {};
        item.id = 2;
        item.value = "已过期 " + this.ended;
        this.tags.push(item);
      });
      let params3 = new URLSearchParams();
      params3.append("cid", this.cid);
      params3.append("status", 0);
      countByStatus(params3).then((response) => {
        this.paused = response.data;
        let item = {};
        item.id = 0;
        item.value = "暂停中 " + this.paused;
        this.tags.push(item);
      });
    },
    selectTag(tag_id, tag_val) {
      if (this.curTag === tag_val) this.curTag = "";
      else this.curTag = tag_val;

      if (this.curTag != "") {
        let params = new URLSearchParams();
        params.append("cid", this.cid);
        params.append("status", tag_id);
        fetchTaskWithStatus(params).then((response) => {
          this.tasks.splice(0, this.tasks.length);
          let data = response.data;
          for (var i = 0; i < data.length; i++) {
            let item = [];
            item.id = this.list[i].id;
            item.title = this.list[i].title;
            item.stime = formatDate(this.list[i].createTime);
            item.etime = formatDate(this.list[i].deadline);
            item.status = this.list[i].status === 0 ? "未开始" : "进行中";
            this.tasks.push(item);
          }
        });
      } else {
        this.tasks.splice(0, this.tasks.length);
        this.getTasks();
      }
    },
    getTasks() {
      colTaskList(this.cid).then((response) => {
        this.list = response.data;
        for (var i = 0; i < this.list.length; i++) {
          let item = {};
          item.id = this.list[i].id;
          item.title = this.list[i].title;
          item.stime = formatDate(this.list[i].createTime);
          item.etime = formatDate(this.list[i].deadline);
          item.status = this.list[i].status === 0 ? "未开始" : "进行中";
          this.tasks.push(item);
        }
      });
    },
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
      if (keyPath[1] === "1-2") {
        this.$router.push({
          path: "/crtask",
          query: { cid: this.cid },
        });
      } else if (keyPath[1] === "1-1") {
        this.$router.push({
          path: "/coltask",
          query: { cid: this.cid },
        });
      } else if (keyPath[1] === "2-1") {
        this.$router.push({
          path: "/colsub",
          query: { cid: this.cid },
        });
      } else if (keyPath[1] === "2-2") {
        this.$router.push({
          path: "/crsub",
          query: { cid: this.cid },
        });
      } else if (keyPath[1] === "3-1") {
        this.$router.push({
          path: "/crtag",
          query: { cid: this.cid },
        });
      }
    },
  },
};
</script>

<style scoped>
.tag {
  margin-right: 5px;
  font-size: 90%;
  display: inline;
  padding: 0.2em 0.6em 0.3em;
  font-weight: bold;
  line-height: 1;
  color: #ffffff;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: 0.25em;
}
.tag_active {
  background-color: #409eff;
}

.tag_unactive {
  background-color: #3d4450;
}

.sub_tag a {
  color: #ecf0f1;
}
.task-item {
  padding: 10px;
  background-color: rgba(212, 159, 233, 0.5);
  margin-bottom: 40px;
  border-radius: 4px;
  -webkit-box-shadow: 0px 0px 10px #656466;
  -moz-box-shadow: 0px 0px 10px #656466;
  box-shadow: 0px 0px 10px #656466;
}

.task-item .th {
  float: left;
}

.task-item .tbtn {
  float: right;
  margin-top: 10px;
}

.task-item .tbtn .btn {
  margin: 10px;
  color: white !important;
  background-color: #3d4450;
  border-color: #3d4450;
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}

#task-footer {
  display: block;
  text-align: center;
  color: #ecf0f1;
  padding: 50px;
  background-color: #3d4450;
}
#task-footer a {
  color: #ecf0f1;
}

#task-footer {
  display: block;
  text-align: center;
  color: #ecf0f1;
  padding: 50px;
  background-color: #3d4450;
}
#task-footer a {
  color: #ecf0f1;
}

#sidebar aside > div {
  border-radius: 4px;
  box-shadow: 1px -1px 6px rgba(155, 89, 182, 0.5);
  -webkit-box-shadow: 1px -1px 6px rgba(155, 89, 182, 0.5);
  background-color: white;
}

.panel {
  margin-bottom: 20px;
  border: 1px solid transparent;
}

.panel-heading {
  border-bottom: none;
  font-size: 14px;
  line-height: 14px;
  font-weight: normal;
}
.panel-heading {
  padding: 10px 15px;
  border-bottom: 1px solid transparent;
  border-top-right-radius: 3px;
  border-top-left-radius: 3px;
}
.login-panel {
  height: 100px;
  padding: 20px 0px;
}
.text-center {
  text-align: center;
}
.login-panel img {
  margin-right: 20px;
  border-radius: 50%;
  border: 2px solid white;
  background-color: white;
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}
img {
  max-width: 100%;
  border-radius: 4px;
  vertical-align: middle;
  height: auto;
}
.login-panel .user-name {
  margin-right: 20px;
}
.btn-inverse-primary {
  color: white !important;
  background-color: #3d4450;
  border-color: #3d4450;
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}
.task-panel a {
  color: #666666;
}
.btn {
  display: inline-block;
  padding: 6px 12px;
  margin-bottom: 0;
  font-size: 12px;
  font-weight: normal;
  line-height: 1.428571429;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 4px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
}
.btn-danger {
  color: #ffffff;
  background-color: #e74c3c;
  border-color: #e74c3c;
  transition: all 0.25s ease-in-out;
  -webkit-transition: all 0.25s ease-in-out;
  -moz-transition: all 0.25s ease-in-out;
  -o-transition: all 0.25s ease-in-out;
}
.el-header {
  background-color: #3d4450;
  border-color: #080808;
  top: 0;
  z-index: 1030;
}
#header {
  display: fixed;
  padding: 0;
  min-height: 50px;
}

.container {
  max-width: 1170px;
  margin-right: auto;
  margin-left: auto;
}

.el-menu {
  background-color: #3d4450;
  font-size: 14px;
  display: block;
}

.el-menu::after,
.el-menu::before {
  display: none;
}

.navbar-brand {
  margin-left: -15px;
  width: 200px;
  height: 50px;
  display: block;
  float: left;
  font-size: 40px;
  color: white;
  text-align: center;
}

.navbar-collapse {
  margin-left: -15px;
  margin-right: -15px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

#boder {
  padding: 30px 0;
}

.container {
  padding-left: 30px;
  padding-right: 20px;
}

.row {
  margin-right: -15px;
  margin-left: -15px;
}

#mainstay {
  width: 66.666666666666%;
  float: left;
  padding-right: 15px;
  padding-left: 15px;
}

#sidebar {
  width: 33.333333333333%;
}
body {
  word-wrap: break-word;
}
body {
  font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;
  font-size: 12px;
  line-height: 1.428571429;
  color: #3d4450;
}
a {
  text-decoration: none;
}

.ct {
  border-radius: 4px;
  box-shadow: -1px -1px 10px rgba(155, 89, 182, 0.5);
  -webkit-box-shadow: -1px -1px 10px rgba(155, 89, 182, 0.5);
  position: relative;
  margin-bottom: 30px;
  padding-bottom: 20px;
  min-height: 1000px;
  padding-bottom: 19px;
  margin-bottom: 30px;
  padding: 19px;
  border-radius: 6px;
  background-color: #ffffff;
  -webkit-box-shadow: 0px 0px 10px #9b59b6;
  -moz-box-shadow: 0px 0px 10px #9b59b6;
  box-shadow: 0px 0px 10px #9b59b6;
}

.crinfo-btn {
  text-align: center;
}
</style>